package com.ruoyi.web.controller.djb;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.djb.domain.DjbDistrictBluetooth;
import com.ruoyi.djb.domain.DjbGate;
import com.ruoyi.djb.service.IDjbDistrictBluetoothService;
import com.ruoyi.djb.service.IDjbGateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 蓝牙设备管理Controller
 * 
 * @author xxd
 * @date 2024-06-13
 */
@Controller
@RequestMapping("/djb/bluetooth")
public class DjbDistrictBluetoothController extends BaseController
{
    private String prefix = "djb/bluetooth";

    @Autowired
    private IDjbDistrictBluetoothService djbDistrictBluetoothService;
    @Autowired
    private IDjbGateService djbGateService;

    @RequiresPermissions("djb:bluetooth:view")
    @GetMapping()
    public String bluetooth()
    {
        return prefix + "/bluetooth";
    }

    /**
     * 查询蓝牙设备管理列表
     */
    @RequiresPermissions("djb:bluetooth:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DjbDistrictBluetooth djbDistrictBluetooth)
    {
        startPage();
        List<DjbDistrictBluetooth> list = djbDistrictBluetoothService.selectDjbDistrictBluetoothList(djbDistrictBluetooth);
        for(DjbDistrictBluetooth tmp: list){
            if(tmp.getGateId()!=null){
                DjbGate gate = djbGateService.selectDjbGateById(tmp.getGateId());
                if(gate!=null) tmp.setGateName(gate.getName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出蓝牙设备管理列表
     */
    @RequiresPermissions("djb:bluetooth:export")
    @Log(title = "蓝牙设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DjbDistrictBluetooth djbDistrictBluetooth)
    {
        List<DjbDistrictBluetooth> list = djbDistrictBluetoothService.selectDjbDistrictBluetoothList(djbDistrictBluetooth);
        ExcelUtil<DjbDistrictBluetooth> util = new ExcelUtil<DjbDistrictBluetooth>(DjbDistrictBluetooth.class);
        return util.exportExcel(list, "蓝牙设备管理数据");
    }

    /**
     * 新增蓝牙设备管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存蓝牙设备管理
     */
    @RequiresPermissions("djb:bluetooth:add")
    @Log(title = "蓝牙设备管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DjbDistrictBluetooth djbDistrictBluetooth)
    {
        return toAjax(djbDistrictBluetoothService.insertDjbDistrictBluetooth(djbDistrictBluetooth));
    }

    /**
     * 修改蓝牙设备管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        DjbDistrictBluetooth djbDistrictBluetooth = djbDistrictBluetoothService.selectDjbDistrictBluetoothById(id);
        if(djbDistrictBluetooth.getGateId()!=null){
            DjbGate gate = djbGateService.selectDjbGateById(djbDistrictBluetooth.getGateId());
            if(gate!=null) djbDistrictBluetooth.setGateName(gate.getName());
        }
        mmap.put("djbDistrictBluetooth", djbDistrictBluetooth);
        return prefix + "/edit";
    }

    /**
     * 修改保存蓝牙设备管理
     */
    @RequiresPermissions("djb:bluetooth:edit")
    @Log(title = "蓝牙设备管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DjbDistrictBluetooth djbDistrictBluetooth)
    {
        return toAjax(djbDistrictBluetoothService.updateDjbDistrictBluetooth(djbDistrictBluetooth));
    }

    /**
     * 删除蓝牙设备管理
     */
    @RequiresPermissions("djb:bluetooth:remove")
    @Log(title = "蓝牙设备管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(djbDistrictBluetoothService.deleteDjbDistrictBluetoothByIds(ids));
    }
}
